import PetownersApi from "../services/Petowners"
import OrderApi from "../services/order"

export default {
    namespaced: true,
    state: {
        data: [],
        order: [],
        orderUpdate: {},
        PetownersUpdate: {},
        total: 0,
        isLoading: false,//加载
        condition: {
            page: 1,
            limit: 3,
            key: "",
            field: "",
            sort: ""
        }
    },
    mutations: {
        setOrder(state, order) {
            state.order = order;
        },
        setOrderUpdate(state, orderUpdate) {
            state.orderUpdate = orderUpdate;
        },
        deleteOrder(state, id) {
            state.order = state.order.filter(it => it._id !== id)
        },
        addOrder(state, order) {
            state.order = order;
        },
        setIsLoading(state, isLoading) {
            state.isLoading = isLoading;
        },
        setCondition(state, condition) {
            state.condition = {
                ...state.condition,
                ...condition
            }
        },
        setTotal(state, total) {
            state.total = total
        },
        //将数据存到仓库
        data(state, petowners) {
            state.data = petowners
        },
        deletePetowners(state, id) {
            state.data = state.data.filter(it => it._id !== id)
        },
        getByIdUser(state, p) {
            state.PetownersUpdate = p
        },
        setEditUser(state, PetownersUpdate) {
            state.PetownersUpdate = PetownersUpdate
        }
    },
    actions: {
        async getByOrderId(context, id) {
            const resp = await OrderApi.getByOrderId(id);
            context.commit("setOrderUpdate", resp.data)
        },
        async getOrder(context, id) {
            const resp = await OrderApi.getOrder(id);
            context.commit("setOrder", resp.data);
        },
        async OrderUp(context, order) {
            await OrderApi.getOrderUpdate(order);
        },
        async deleteOr(context, id) {
            await OrderApi.deleteOrder(id);
        },
        async addOrder(context, order) {
            const resp = await OrderApi.addOrder(order);
            context.commit("addOrder", resp.data);
        },
        async data(context) {
            context.commit("setIsLoading", true);
            const resp = await PetownersApi.getPetowners(context.state.condition)
            context.commit("data", resp.data);
            context.commit('setTotal', resp.count);
            context.commit("setIsLoading", false);
        },
        async addPetowners(context, Petowners) {
            await PetownersApi.addPetowners(Petowners)
        },
        async deletePetowners(context, id) {
            await PetownersApi.deletePetowners(id);
            context.commit("deletePetowners", id)
        },
        async getPetownersById(context, id) {
            const resp = await PetownersApi.getPetownersById(id);
            context.commit("setEditUser", resp.data)
        },
        async updatePetowners(context, Petowners) {
            await PetownersApi.updatePetowners(Petowners);
        }
    }
}